var app = getApp()
Page({
    data: {
        code: 0,
        pagelist: {},
        page: 1,
        pageEnd: false,
        cid: 0,
        sortby: 1,
        keyword: '',
        showAny: false,
        showAny2: false
    },

    onShow() {
        var that = this
        if (typeof this.getTabBar === 'function' &&
            that.getTabBar()) {
            that.getTabBar().setData({
                selected: 1
            })
        }
    },

    onLoad() {
        this.getlist()
    },

    getlist() {
        var that = this
        const { page,cid,keyword,sortby } = this.data
        const data = { page,cid,keyword,sortby }
        app.api_request('?c=coach','GET',data).then(function (res) {
            const pagelist = res
            if (pagelist) {
                const { pagelist: pinfo } = that.data
                if (pinfo.listcoach) {
                    pagelist.listcoach = pinfo.listcoach.concat(pagelist.listcoach)
                }
                const data = {
                    code: 1,
                    pagelist,
                    listsort: pagelist.listsort,
                    pageEnd: page == pagelist.pages
                }
                if (!that.data.sortby) {
                    data.sortby = pagelist.listsort[0].id
                }
                that.setData(data)
                tt.setNavigationBarTitle({
                    title: pagelist.appTitle
                })
            }
        })
    },

    onReachBottom() {
        var that = this
        const { page,pageEnd } = that.data
        if (!pageEnd) {
            tt.showLoading({
                title: '正在加载…',
            })
            setTimeout(function () {
                that.setData({
                    page: page + 1
                }, () => {
                    that.getlist()
                })
                tt.hideLoading();
            },300)
        }
    },

    onSwitchTab(e) {
        const { sortby } = e.currentTarget.dataset
        this.setData({
            pagelist: {},
            sortby: sortby,
            page: 1
        },() => {
            this.getlist()
        })
    },

    onSearch(e) {
        var that = this
        const { value } = e.detail
        tt.showLoading({
            title: '正在加载…',
        })
        setTimeout(function () {
            that.setData({
                pagelist: {},
                page: 1,
                keyword: value,
            },() => {
                that.getlist()
                that.closeAny()
            })
            tt.hideLoading();
        },300)
    },

    selgid(e) {
        const { id } = e.currentTarget.dataset
        this.setData({
            pagelist: {},
            cid: id,
            page: 1
        },() => {
            this.getlist()
            this.closeAny()
        })
    },

    showAny() {
        this.setData({
            showAny: true
        })
    },

    showAny2() {
        this.setData({
            showAny2: true
        })
    },

    closeAny() {
        this.setData({
            showAny: false,
            showAny2: false
        })
    },

    reset() {
        this.setData({
            pagelist: {},
            cid: 0,
            keyword: '',
            page: 1
        },() => {
            this.getlist()
        })
    },

    onShareAppMessage: function () {
        var that = this
        return {
            title: that.data.pagelist.appTitle,
            desc: that.data.pagelist.appDesc,
            path: '/pages/coach/index'
        }
    }
})